﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bet_Yatziv
{
    public partial class Employees : Form
    {
        //init
        public Employees()
        {
            InitializeComponent();
            refresh();
        }

        //loading info
        private void btn_load(object sender, EventArgs e)
        {
            DataTable table = DataAccessLayer.GetAllEmployees();
            dataGridView1.DataSource = table;
        }

        //returns to menu
        private void btn_return(object sender, EventArgs e)
        {
            this.Close();
        }

        //clear
        private void btn_clear(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            clearAll();
            disableText();
            unshowOK();
            mainAdd.Enabled = true;
            mainUpdate.Enabled = true;
            mainDelete.Enabled = true;
        }

        //update employee details
        private void Updatee()
        {
            string id = txt_IDE.Text;
            string firstName = txt_fNameE.Text;
            string lastName = txt_lNameE.Text;
            string address = txt_addressE.Text;
            string cellPhone = txt_cellPhoneE.Text;
            string wage = txt_wage.Text;

            DateTime bdate = txt_date.Value;
            int wageNum = 0;
            int cellNum = 0;
            int idNum = 0;

            //converting id to number
            if (id.Length > 0)
            {
                try
                {
                    idNum = Convert.ToInt32(id);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "נא להזין תז תקין", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_IDE.Text = "";
                    return;
                }
            }

            //convrting wage to number
            if (wage.Length > 0)
            {
                try
                {
                    wageNum = Convert.ToInt32(wage);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "נא להכניס שכר כמספר חיובי גדול מ0", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_wage.Text = "";
                    return;
                }
            }

            //converting cell phone to number
            if (cellPhone.Length > 0)
            {
                try
                {
                    cellNum = Convert.ToInt32(cellPhone);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "נא להכניס מס פלאפון כמספר", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_cellPhoneE.Text = "";
                    return;
                }
            }

            if (employeeSearch(idNum))
            {
                DataAccessLayer.UpdateEmp(idNum, firstName, lastName, address, cellPhone, wageNum, bdate);
                MessageBox.Show("פרטי עובד עודכנו בהצלחה", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                refresh();
            }
            else
            {
                MessageBox.Show("עובד לא קיים", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //add employee
        private void Add()
        {
            try
            {
                string id = txt_IDE.Text;
                string firstName = txt_fNameE.Text;
                string lastName = txt_lNameE.Text;
                string address = txt_addressE.Text;
                string cellPhone = txt_cellPhoneE.Text;
                string wage = txt_wage.Text;
                DateTime bdate = txt_date.Value;

                if ((id == "") || (firstName == "") || (lastName == "") || (address == "") || (cellPhone == ""))

                    MessageBox.Show("נא מלא/י את כל פרטי העובד", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);

                else
                {
                    int cellNum = 0;
                    int wageNum = 0;
                    int idNum = 0;

                    //converting id to number
                    if (id.Length > 0)
                    {
                        try
                        {
                            idNum = Convert.ToInt32(id);
                        }
                        catch (FormatException exp)
                        {
                            MessageBox.Show(exp.Message + "נא להכניס תעודת זהות במספרים", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txt_IDE.Text = "";
                            return;
                        }
                    }

                    //converting wage to number
                    if (wage.Length > 0)
                    {
                        try
                        {
                            wageNum = Convert.ToInt32(wage);
                        }
                        catch (FormatException exp)
                        {
                            MessageBox.Show(exp.Message + "נא להכניס שכר כמספר חיובי גדול מ0", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txt_wage.Text = "";
                            return;
                        }
                    }

                    //converting cellphone to number
                    if (cellPhone.Length > 0)
                    {
                        try
                        {
                            cellNum = Convert.ToInt32(cellPhone);
                        }
                        catch (FormatException exp)
                        {
                            MessageBox.Show(exp.Message + "מספר פלאפון יכול להכיל ספרות בלבד", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txt_cellPhoneE.Text = "";
                            return;
                        }
                    }

                    if (employeeSearch(idNum))
                    {

                        MessageBox.Show("העובד כבר קיים במערכת", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {

                        DataAccessLayer.AddNewEmp(idNum, firstName, lastName, address, cellPhone, wageNum, bdate, 0);
                        MessageBox.Show("פרטי עובד עודכנו בהצלחה", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        refresh();
                    }
                }
            }
            catch
            {
                MessageBox.Show("אחד מן השדות שהוזנו שגויים", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        //delete employee
        private void Delete()
        {
            string id = txt_IDE.Text;
            int idNum = 0;

            //converting id to number
            if (id.Length > 0)
            {
                try
                {
                    idNum = Convert.ToInt32(id);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "נא להכניס מספר תז תקין", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_IDE.Text = "";
                    return;
                }
            }

            if (employeeSearch(idNum))
            {
                if (MessageBox.Show("מחיקת העובד תמחק את כל המידע הרלוונטי אליו במערכת, האם אתה בטוח שברצונך למחוק את העובד", "מחיקת עובד", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DataAccessLayer.DeleteEmp(idNum);
                    MessageBox.Show("עובד נמחק בהצלחה", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    refresh();
                }
            }
            else
            {
                MessageBox.Show("עובד לא קיים", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //refreshing data and text boxes
        private void refresh()
        {
            dataGridView1.DataSource = null;
            DataTable dt = DataAccessLayer.GetAllEmployees();
            dataGridView1.DataSource = dt;
            clearAll();

            string query1 = "select employeeID from tbl_employees";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);

            txt_IDE.Items.Clear();
            for (int i = 0; i < dt1.Rows.Count; i++)
                txt_IDE.Items.Add(dt1.Rows[i].ItemArray[0]);
        }
        
        //main delete button
        private void btn_delete(object sender, EventArgs e)
        {
            showOK();
            disableText();
            mainAdd.Enabled = true;
            mainUpdate.Enabled = true;
            mainDelete.Enabled = false;
        }

        //cancel button
        private void btn_cancel(object sender, EventArgs e)
        {
            unshowOK();
            disableText();       
            mainAdd.Enabled = true;
            mainUpdate.Enabled = true;
            mainDelete.Enabled = true;
            clearAll();
        }

        //main add button
        private void btn_add(object sender, EventArgs e)
        {
            showOK();
            enableText();
            mainAdd.Enabled = false;
            mainUpdate.Enabled = true;
            mainDelete.Enabled = true;
            clearAll();
        }

        //main update button
        private void btn_update(object sender, EventArgs e)
        {
            enableText();
            showOK();
            mainAdd.Enabled = true;
            mainUpdate.Enabled = false;
            mainDelete.Enabled = true;
        }

        //bringing up all employee details when choosing him
        private void txt_IDE_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            string query1 = "select * from tbl_employees";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);
            int ideNum = 0;
            if (txt_IDE.Text.Length > 0)
            {
                try
                {
                    ideNum = Convert.ToInt32(txt_IDE.Text);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "אנא הכנס מספר תעודת זהות תקין", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_IDE.Text = "";
                    return;
                }
            }

            for (i = 0; i < dt1.Rows.Count; i++)
                if (ideNum == (int)(dt1.Rows[i].ItemArray[0]))
                    break;

            txt_fNameE.Text = "" + dt1.Rows[i].ItemArray[1];
            txt_lNameE.Text = "" + dt1.Rows[i].ItemArray[2];
            txt_addressE.Text = "" + dt1.Rows[i].ItemArray[3];
            txt_cellPhoneE.Text = "" + dt1.Rows[i].ItemArray[4];
            txt_wage.Text = "" + dt1.Rows[i].ItemArray[5];
            txt_date.Text = "" + dt1.Rows[i].ItemArray[6];
        }

        //clearing all txt fields
        private void clearAll()
        {
            txt_fNameE.Text = txt_lNameE.Text = txt_IDE.Text = txt_addressE.Text = "";
            txt_cellPhoneE.Text = txt_wage.Text = "";
        }

        //ok button
        private void btn_ok(object sender, EventArgs e)
        {
            if (mainDelete.Enabled == false)
                Delete();
            if (mainUpdate.Enabled == false)
                Updatee();
            if (mainAdd.Enabled == false)
                Add();
        }

        //enabling ok buttons
        public void showOK()
        {
            deleteCanc.Visible = true;
            Badd.Visible = true;
        }

        //disabling ok buttons
        public void unshowOK()
        {
            deleteCanc.Visible = false;
            Badd.Visible = false;
        }

        //enabling all textboxes
        public void enableText()
        {
            txt_addressE.Enabled = true;
            txt_cellPhoneE.Enabled = true;
            txt_date.Enabled = true;
            txt_fNameE.Enabled = true;
            txt_lNameE.Enabled = true;
            txt_wage.Enabled = true;
        }

        //disabling all textboxes
        public void disableText()
        {
            txt_addressE.Enabled = false;
            txt_cellPhoneE.Enabled = false;
            txt_date.Enabled = false;
            txt_fNameE.Enabled = false;
            txt_lNameE.Enabled = false;
            txt_wage.Enabled = false;
        }

        //searching employee
        public Boolean employeeSearch(int idNum)
        {
            int i = 0;
            string query1 = "select employeeID from tbl_employees";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);
            for (i = 0; i < dt1.Rows.Count; i++)
                if (idNum == (int)(dt1.Rows[i].ItemArray[0]))
                    break;

            if (i >= 0 && i < dt1.Rows.Count)
                return true;
            else
                return false;
        }
    }
}